# strategy_signal.py
from enum import Enum, auto
from dataclasses import dataclass, field
from typing import Optional, Union
from datetime import datetime  # 确保 datetime 被导入


class SignalType(Enum):
    HOLD = auto()  # 无操作，继续持有
    # ... (其他枚举值不变) ...
    OPEN_LONG = auto()  # 开多仓
    OPEN_SHORT = auto()  # 开空仓
    CLOSE_LONG = auto()  # 平多仓 (全部或部分)
    CLOSE_SHORT = auto()  # 平空仓 (全部或部分)
    EXIT_ALL = auto()  # 平掉所有当前持仓
    ADD_TO_LONG = auto()  # 加仓多单
    ADD_TO_SHORT = auto()  # 加仓空单
    REDUCE_LONG = auto()  # 减仓多单 (部分平仓)
    REDUCE_SHORT = auto()  # 减仓空单 (部分平仓)
    STOP_LOSS_LONG = auto()  # 多单止损
    STOP_LOSS_SHORT = auto()  # 空单止损
    TAKE_PROFIT_LONG = auto()  # 多单止盈
    TAKE_PROFIT_SHORT = auto()  # 空单止盈


@dataclass
class StrategySignal:
    """
    表示由策略生成的交易信号。
    """
    signal_type: SignalType
    timestamp: datetime  # <--- 类型是 datetime
    symbol: str  # 交易对符号，例如 "ETHUSDT"

    price: Optional[float] = None
    size: Optional[float] = None
    stop_loss_price: Optional[float] = None
    take_profit_price: Optional[float] = None
    comment: Optional[str] = None
    order_id: Optional[str] = None

    def __str__(self):
        # 字符串表示方法不变
        details = f"信号: {self.signal_type.name} 交易对: {self.symbol} 时间: {self.timestamp.strftime('%Y-%m-%d %H:%M:%S UTC')}"
        if self.price is not None:
            details += f", 价格: {self.price:.4f}"
        if self.size is not None:
            details += f", 数量: {self.size}"
        if self.stop_loss_price is not None:
            details += f", 止损价: {self.stop_loss_price:.4f}"
        if self.take_profit_price is not None:
            details += f", 止盈价: {self.take_profit_price:.4f}"
        if self.comment:
            details += f", 备注: {self.comment}"
        return details
